package com.example.children.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.children.DTO.AttendanceTeacherDTO;
import com.example.children.DTO.BehaviorTeacherDTO;
import com.example.children.Model.Attendance;
import com.example.children.Model.Behavior;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface BehaviorMapper extends BaseMapper<Behavior> {

    @Select("select * from Behavior where student_id=#{student_id}") //根据学生id来查找他的所有的日常行为记录
    List<Behavior> selectByStudentId(String student_id);



    //根据日期查询日常
    //日期查询
    @Select("SELECT * FROM Behavior WHERE student_id = #{studentId} AND `date` LIKE CONCAT('%', #{date}, '%') ORDER BY date")
    List<Behavior> selectByDateSend(Behavior behavior);


    // 查询总记录数
    @Select("SELECT COUNT(*) FROM Behavior s WHERE s.behavior_id IS NOT NULL")
    int countTotalRecords();

    // 手动分页查询
    @Select("SELECT " +
            "s.student_id AS studentId, s.behavior_id AS behaviorId, s.date,ss.name as studentName, " +
            "s.behavior_description as behaviorDescription, s.teacher_comments as teacherComments, cls.teacher_id AS teacherId, ct.class_name AS className, c.class_id AS classId, " +
            "t.teacher_name as teacherName,t.teacher_phone as teacherPhone "+
            "FROM Behavior s " +
            "JOIN student ss ON s.student_id = ss.id " +
            "left JOIN class c ON s.student_id = c.student_id " +
            "left JOIN classTo ct ON ct.class_id = c.class_id " +
            "left JOIN classteacher cls ON cls.class_id = c.class_id " +
            "left JOIN teacher t ON t.id = cls.teacher_id " +
            "WHERE s.behavior_id IS NOT NULL AND (cls.headteacher = '班主任' OR cls.headteacher IS NULL) " +
            "ORDER BY s.date " +
            "LIMIT #{pageSize} OFFSET #{offset}")
    List<BehaviorTeacherDTO> GetAllBehaviorTeacherDTO(@Param("offset") int offset, @Param("pageSize") int pageSize);



    //admin查询
    @Select("SELECT DATE_FORMAT(a.date,'%Y-%m-%d') as date,a.teacher_comments as teacherComments,s.id as studentId,s.name as studentName,a.behavior_id as behaviorId,a.behavior_description as behaviorDescription, " +
            "t.id as teacherId,t.teacher_name as teacherName,t.teacher_phone as teacherPhone,c.class_id as classId, " +
            "ct.class_name as className " +
            "FROM Behavior a " +
            "JOIN student s ON a.student_id = s.id " +
            "LEFT JOIN class c ON c.student_id = s.id " +
            "LEFT JOIN classTo ct ON c.class_id = ct.class_id " +
            "LEFT JOIN classteacher cls ON cls.class_id = c.class_id " +
            "LEFT JOIN teacher t ON t.id = cls.teacher_id " +
            "WHERE (#{studentId} IS NULL OR a.student_id LIKE CONCAT('%', #{studentId}, '%')) " +
            "AND (#{date} IS NULL OR a.date LIKE CONCAT('%', #{date}, '%')) " +
            "AND (#{studentName} IS NULL OR s.name LIKE CONCAT('%', #{studentName}, '%')) " +
            "ORDER BY a.date")
    List<BehaviorTeacherDTO> AdminSelectByDateSend(BehaviorTeacherDTO behaviorTeacherDTO);
}
